package com.luckmgr.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.common.constant.SystemConstant;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.luckmgr.entity.db.Company;
import com.luckmgr.entity.db.User;
import com.luckmgr.service.CompanyService;


@Controller
@RequestMapping("/company")
public class CompanyController {
	
	@Autowired
	CompanyService companyServiceImpl;
	
	@RequestMapping(value = "")
	public String companymain(Model model,HttpSession session,String comname,@RequestParam(required = false)  Integer page) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(SystemConstant.ADMINISTRATOR_CODE != user.getUserLevel()){
			model.addAttribute("msg", "你没有查询该数据的权限！");
			return "company/comindex";
		}
		if(null == page)
			page = 1;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", comname);
		PageBean<Company> pagebean = companyServiceImpl.selectListPage(map, page);
		model.addAttribute("pagebean", pagebean);
		model.addAttribute("comname", comname);
		//查询公司列表 密是admin的管理员
		return "company/comindex";
	}
	
	
	@RequestMapping(value = "addup")
	public String comaddup(Model model,HttpSession session,Integer id) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(SystemConstant.ADMINISTRATOR_CODE != user.getUserLevel()){
			model.addAttribute("msg", "你没有操作该数据的权限！");
			return "company/comindex";
		}
		
		Company company = null;
		if(null != id && 0 != id){
			company = companyServiceImpl.selectByPrimaryKey(id);
		}
		if(company!=null){
			model.addAttribute("company", company);
		}
		return "company/comadd";
	}

	
	@RequestMapping(value = "delete")
	public String comdelete(Model model,HttpSession session,@RequestParam(required = false)Integer id) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(SystemConstant.ADMINISTRATOR_CODE != user.getUserLevel()){
			model.addAttribute("msg", "你没有操作该数据的权限！");
			return "company/comindex";
		}
		if(null != id && 0 != id){
			companyServiceImpl.deleteByPrimaryKey(id);
		}
		return "redirect:/company";
	}

	
	
	@RequestMapping(value = "update")
	public String companyupdate(Model model,HttpSession session,@ModelAttribute("company") Company company) {
		User user = (User) session.getAttribute(SystemConstant.ESSIONUSER);
		if(SystemConstant.ADMINISTRATOR_CODE != user.getUserLevel()){
			model.addAttribute("msg", "你没有操作该数据的权限！");
			return "company/comindex";
		}
		if(StringUtils.isNull(company.getName()) || StringUtils.isNull(company.getCode())){
			model.addAttribute("msg", "数据不完整请重新录入！");
			return "company/comadd";
		}
		
		company.setOperatorName(user.getUserName());
		company.setOperatorIp(user.getLoginIp());
		
		if(company.getId()!=null && 0!=company.getId()){
			companyServiceImpl.updateEntity(company);
		}else{
			companyServiceImpl.addEntity(company);
		}
		return "redirect:/company";
	}
	
	
	
}
